package com.study.gupao.proxy.cglibproxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @ClassName: CglibProxy
 * @Description:
 * @author: Adminstrator
 * @Date: 2019-03-13 22:21
 */
public class CglibProxy implements MethodInterceptor {

    public  <T>T newProyxInstance(Class<T> clazz){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return (T)enhancer.create();
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {

        System.out.println("之前");
        Object o = proxy.invokeSuper(obj, args);
        System.out.println("之后");
        return o;
    }
}
